﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AFT.Parking.WebSite.Common
{
    public static class EnumHelper
    {
        /// <summary>
        /// 枚举转列表,以便绑定数据
        /// </summary>
        /// <param name="enumType">typeof(枚举)</param>
        /// <param name="SelectId">选中的id</param>
        /// <param name="HasPleaseSelect">true:有《请选择》;false:反之</param>
        /// <returns></returns>
        public static List<SelectListItem> EnumToList(this Type enumType, int SelectId, bool HasPleaseSelect)
        {
            var list = new List<SelectListItem>();
            SelectListItem sli;
            if (HasPleaseSelect)
            {
                sli = new SelectListItem()
                {
                    Text = "请选择",
                    Value = "-1"
                };
                list.Add(sli);
            }
            string[] names = Enum.GetNames(enumType);
            Array values = Enum.GetValues(enumType);
            for (int i = 0; i < names.Length; i++)
            {
                int value = (int)values.GetValue(i);
                sli = new SelectListItem()
                {
                    Text = names[i],
                    Value = value.ToString(),
                    Selected = (value == SelectId)
                };
                list.Add(sli);
            }
            return list;
        }
    }
}